package com.xu.mode.Strategy.springStrategy;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
import sun.misc.Contended;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;

/**
 * @version v1.0
 * @类描述：
 * @项目名称：
 * @包名： com.xu.mode.Strategy.springStrategy
 * @类名称：InspectionSolverChooser
 * @创建人：YIHANG.XU
 * @创建时间：2019-08-2416:36
 * @修改人：YIHANG.XU
 * @修改时间：2019-08-2416:36
 * @修改备注：
 * @Copyright
 */
@Component
public class InspectionSolverChooser implements ApplicationContextAware {
    private Map<String, InspectionSolver> chooseMap = new HashMap<>();

    public InspectionSolver choose(String type) {
        return chooseMap.get(type);
    }

    @PostConstruct
    public void registerSS() {
        //getBeansOfType 方法会初始化出当前接口下面的所有子类的实力对象，这个说白的就是根据不同的判断生成不同的对象
        Map<String, InspectionSolver> solverMap = context.getBeansOfType(InspectionSolver.class);
        for (InspectionSolver solver : solverMap.values()) {
            for (String support : solver.supports()) {
                chooseMap.put(support, solver);
            }
        }
    }

    private ApplicationContext context;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.context = applicationContext;
    }
}
